<?php
/**
 * Loads all things needed at start
 *
 *
 * LICENSE: Some license information
 *
 * phpMMB version: 0.0.1
 *
 * $copyright  2007 SaWey
 * $license    GNU GPL v3.
 * $URL: http://www.sawey.be/phpMyMovieBox $
 * $Revision: 6 $
 * $LastChangedBy: sander.weyens $
 * $Date: 2008-01-14 22:20:38 +0000 (Mon, 14 Jan 2008) $
*/

//include required classes
include('includes/config.php');
include('includes/lib_sql_core.php');
include('includes/lib_mysql.php');
include('includes/lib_template.php');
include('includes/lib_acl_auth.php');

//instantiate db
$db		= new ezSQL_mysql($db_user, $db_password, $db_table, $db_host);


//load config from db
$sql_config = 'SELECT * FROM ' . $db_prefix . 'config';
$result = $db->get_results($sql_config, ARRAY_A);
foreach($result as $conf_pair)
{
	$config[$conf_pair['key']] = $conf_pair['value'];
}


//instantiate other classes
include('includes/constants.php');
$user	= new Access_user($db, false);
$script_dir = substr($_SERVER['SCRIPT_FILENAME'], 0, (strlen($_SERVER['SCRIPT_FILENAME']) - 9) );
$tpl	= new FastTemplate($script_dir . 'themes/' . $config['theme']);


//load language
$lang_file = 'language/lang_' . $config['default_language'] . '.php';
if(file_exists($lang_file))
{
	include($lang_file);
}
else
{
	include('language/lang_en.php');

}



//this function creates paging
function create_paging($total, $link, $current_page)
{
	global $config;
	$max = $config['show_movies_per_page'];
	$pages = ceil($total / $max);
	$paging['limit']['start'] = $current_page * $max;
	$paging['limit']['stop'] = $max;
	$paging['nav'] = '';
	
	if(($current_page - 1) >= 0)
	{
		$paging['nav'] .= '<a href="' . $link . '&p=' . ($current_page - 1) . '">< </a>';
	}
	
	for($i = 0; $i < $pages; $i++)
	{
		if($i == $current_page)
		{
			$paging['nav'] .= '<a href="' . $link . '&p=' . $i . '"> <b>' . ($i + 1) . '</b></a>';
		}
		else
		{
			$paging['nav'] .= '<a href="' . $link . '&p=' . $i . '"> ' . ($i + 1) . '</a>';
		}
	}
	if(($current_page + 1) <= ($pages - 1))
	{
		$paging['nav'] .= '<a href="' . $link . '&p=' . ($current_page + 1) . '"> ></a>';
	}
	
	return $paging;	
}

//this function creates a shorter string if the title would become to long
function shorten_string($string)
{
	global $config;
	$max = $config['title_length'];
	if(strlen($string) > $max)
	{
		$string = trim(substr($string, 0, $max)) . '...';
	}
	
	return $string;
}

function get_movie_info($movie_id)
{
	global $config, $db, $db_prefix;
	
	//get all things for this movie
	$sql_movie = 'SELECT m.*, c.country FROM ' . $db_prefix .'movies AS m LEFT JOIN ' . $db_prefix .'countries AS c ON m.country = c.country_id WHERE movie_id = ' . $movie_id;
	$sql_languages = 'SELECT language FROM ' . $db_prefix .'languages AS l LEFT JOIN ' . $db_prefix .'movies_languages AS m_l ON l.language_id = m_l.language_id WHERE m_l.movie_id = ' . $movie_id;
	$sql_awards = 'SELECT award FROM ' . $db_prefix .'awards AS a LEFT JOIN ' . $db_prefix .'movies_awards AS m_a ON a.award_id = m_a.award_id WHERE m_a.movie_id = ' . $movie_id;
	$sql_genres = 'SELECT genre FROM ' . $db_prefix .'genres AS g LEFT JOIN ' . $db_prefix .'movies_genres AS m_g ON g.genre_id = m_g.genre_id WHERE m_g.movie_id = ' . $movie_id;
	$sql_people = 'SELECT p.name, m_p.role, c.name AS char_name FROM ' . $db_prefix .'people AS p '
				. 'LEFT JOIN ' . $db_prefix .'movies_people AS m_p ON p.people_id = m_p.people_id '
				. 'LEFT JOIN ' . $db_prefix .'characters AS c ON m_p.character_id = c.character_id '
				. 'WHERE m_p.movie_id = ' . $movie_id;

	$tmp = $db->get_results($sql_movie, ARRAY_A);
	$movie['main'] = $tmp[0];
	$movie['awards'] = clean_db_array($db->get_results($sql_awards, ARRAY_A), 'award');
	$movie['genres'] = clean_db_array($db->get_results($sql_genres, ARRAY_A), 'genre');
	$movie['languages'] = clean_db_array($db->get_results($sql_languages, ARRAY_A), 'language');
	$movie['people'] = sort_people_array($db->get_results($sql_people, ARRAY_A));
	
	return $movie;
}

function clean_db_array($arr, $field)
{
	if(is_array($arr))
	{
		foreach($arr as $sub_arr)
		{
			$tmp[] = $sub_arr[$field];
		}
	}
	return $tmp;
}

function sort_people_array($arr)
{	
	if(is_array($arr))
	{
		foreach($arr as $person)
		{
			$p['name'] = $person['name'];
			$p['character'] = $person['char_name'];
			$tmp[$person['role']][] = $p;
		}
	}
	return $tmp;
}


















?>